package com.xms.security.api.controller;


import com.xms.core.enums.AccessRightValue;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.security.core.PrivilegeResourceItem;
import com.xms.security.entity.EntityPermission;
import com.xms.security.service.dataauth.IEntityPermissionFinderService;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/{org}/api/security/entitypermission")
public class EntityPermissionController extends ApiBaseController {
    private IEntityPermissionFinderService _entityPermissionFinderService;
    private IEntityFinderService _entityFinderService;
    @Autowired
    public EntityPermissionController(IEntityPermissionFinderService entityPermissionFinderService
            , IEntityFinderService entityFinderService)
    {
        _entityPermissionFinderService = entityPermissionFinderService;
        _entityFinderService = entityFinderService;
    }

    @ApiOperation("查询实体权限资源")
    @GetMapping("privilegeresource")
    public JsonResultObject privilegeResource()
    {
        List<EntityPermission> data = _entityPermissionFinderService.all();
        if (CollectionUtil.notEmpty(data))
        {
            List<PrivilegeResourceItem> result = new ArrayList<>();
            List<Entity> entities = _entityFinderService.all().stream().filter(x -> x.getAuthorizationenabled()).collect(Collectors.toList());
            for (Entity item : entities)
            {
                if (!data.stream().anyMatch(x -> x.getEntityid().equals(item.getEntityid())))
                {
                    continue;
                }
                PrivilegeResourceItem group1 = new PrivilegeResourceItem();
                group1.label = item.getLocalizedname();
                group1.children = new ArrayList<>();
                List<EntityPermission> eps = data.stream().filter(x -> x.getEntityid().equals(item.getEntityid())).collect(Collectors.toList());
                for (EntityPermission ep : eps){
                    PrivilegeResourceItem children = new PrivilegeResourceItem();
                    children.id = ep.getEntitypermissionid();
                    children.label = AccessRightValue.text(ep.getAccessright());
                    children.authorizationenabled = true;
                    group1.children.add(children);
                }
                result.add(group1);
            }
            return JOk(result);
        }
        return JOk();
    }
}
